<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-01 16:40:00
 * @Last Modified by:   lianlianzan
 * @Last Modified time: 2021-02-20 10:50:30
 */

namespace app\weixin_manage\controller;
use app\common\controller\Backend;

class WechatRepair extends Backend {

	function initialize() {
		parent::initialize();
	}

	/**
	 * 批量获取用户的openid
	 * 执行地址，本地：http://www2.kszhuangxiu.com/weixin_manage/wechatRepair/getUserList/
	 * 执行地址，线上：http://www.kszhuangxiu.com/weixin_manage/wechatRepair/getUserList/
	 */
	public function getUserList() {
		$db_perpage = 100; //每页条数
		$wid = 7;

		$weixinAccountDao = model('WechatAccount');
		$wechatUserDao = model('WechatUser');

		$p = input('param.p', '1', '', 'intval');
		$suc = input('param.suc', '', 'intval');
		$lastOpenid = input('param.lastOpenid', '', 'pwEscape');

		//获取公众号信息
		$wxdb = $weixinAccountDao->getInfo($wid);

		$options = array(
			'token'=>$wxdb['token'], //填写你设定的key
			'encodingaeskey'=>$wxdb['aeskey'], //填写加密用的EncodingAESKey
			'appid'=>$wxdb['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
			'appsecret'=>$wxdb['appsecret'], //填写高级调用功能的密钥
			'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
			'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
			'paysignkey'=>'' //商户签名密钥Key，支付权限专用
		);
		$weObj = new \wechat\Wechat($options);

		$result = $weObj->getUserList($lastOpenid); //获取用户列表
		if ($result == false) {
			echo '失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg;exit;
		}

		$data = $result['data'];
		$count = $result['count'];
		$openidArr = $data['openid'];

		$lastOpenid = '';
		foreach ($openidArr as $key => $value) {
			$checkInfo = $wechatUserDao->findMap(['wid'=>$wid, 'openid'=>$value]);
			if (!$checkInfo) {
				$data = array(
					'wid'				=> $wid,
					'openid'			=> $value,
				);
				//print_r($data);exit;
				$wechatUserDao->baseAddData($data); //新增用户
			}
			$lastOpenid = $value;
		}

		if ($count > 0) {
			$p++;
			$this->wapSuccess('同步该页数据成功共同步'.$count.'个数据，成功'.$suc.'个数据，等待跳转', url('weixin_manage/wechatRepair/getUserList', ['p'=>$p, 'suc'=>$suc, 'lastOpenid'=>$lastOpenid]));
		} else {
			$this->wapSuccess('同步用户数据成功');
		}
	}

	/**
	 * 批量获取公众号，已关注的用户信息，获取他们的信息，比如unionid，并更新
	 * SELECT count(*) FROM `wx_weixin_user` WHERE wid=12 and unionid=''
	 * 执行地址，本地：http://dinghuo2.xiezhiba.com/weixin_manage/wechatRepair/repairUserInfoBatch/
	 * 执行地址，线上：http://www.ekunshan.cn/weixin_manage/wechatRepair/repairUserInfoBatch/
	 */
	public function repairUserInfoBatch() {
		$db_perpage = 100; //每页条数
		$wid = 7;

		$weixinAccountDao = model('WechatAccount');
		$wechatUserDao = model('WechatUser');

		$p = input('param.p', '1', '', 'intval');
		$suc = input('param.suc', '', 'intval');

		//读取用户数据
		$map = [
			'wid'		=> $wid,
			'unionid'	=> '',
		];

		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		list($list) = $wechatUserDao->search($map, $Page->getLimit());
		//echo $wechatUserDao->getLastSql();exit;
		//print_r($list);exit;
		$count = count($list);

		//获取公众号信息
		$wxdb = $weixinAccountDao->getInfo($wid);

		$options = array(
			'token'=>$wxdb['token'], //填写你设定的key
			'encodingaeskey'=>$wxdb['aeskey'], //填写加密用的EncodingAESKey
			'appid'=>$wxdb['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
			'appsecret'=>$wxdb['appsecret'], //填写高级调用功能的密钥
			'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
			'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
			'paysignkey'=>'' //商户签名密钥Key，支付权限专用
		);
		$weObj = new \wechat\Wechat($options);

		$user_list = [];
		foreach ($list as $key => $value) {
			$tmp = array(
				'openid'	=> $value['openid'],
				'lang'		=> 'zh_CN'
			);
			$user_list[] = $tmp;
		}
		//print_r($user_list);exit;

		if (count($user_list) > 0) {
			$paramData = [];
			$paramData['user_list'] = $user_list;

			$result = $weObj->getUserInfoBatch($paramData); //批量获取关注者详细信息
			if ($result == false) {
				echo '失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg;exit;
			}
			//print_r($result);exit;

			$resultUserList = $result['user_info_list'];
			foreach ($resultUserList as $key => $wxUserInfo) {
				$data = array(
					'nickname'			=> $wxUserInfo['nickname'],
					'sex' 				=> $wxUserInfo['sex'],
					'city' 				=> $wxUserInfo['city'],
					'province' 			=> $wxUserInfo['province'],
					'country' 			=> $wxUserInfo['country'],
					'headimgurl' 		=> $wxUserInfo['headimgurl'],
					'subscribe_time'	=> $wxUserInfo['subscribe_time'],
					'unionid'			=> $wxUserInfo['unionid'],
				);
				//更新每个用户(根据wid和openid更新)
				$wechatUserDao->where(array('wid'=>$wid, 'openid'=>$wxUserInfo['openid']))->data($data)->save();
				//echo $wechatUserDao->getLastSql();exit;
			}
			//print_r($resultUserList);exit;
		}

		if ($count > 0) {
			$p++;
			$this->wapSuccess('同步该页数据成功共同步'.$count.'个数据，成功'.$suc.'个数据，等待跳转', url('weixin_manage/wechatRepair/repairUserInfoBatch', ['p'=>$p, 'suc'=>$suc]));
		} else {
			$this->wapSuccess('同步用户详细信息成功');
		}
	}

	/**
	 * 单个获取公众号，已关注的用户信息，获取他的信息，比如unionid，并更新
	 * SELECT count(*) FROM `wx_weixin_user` WHERE wid=12 and unionid=''
	 * 执行地址，本地：http://www2.kszhuangxiu.com/manage/wechatRepair/repairUnionidSingle/
	 * 执行地址，线上：http://www.kszhuangxiu.com/manage/wechatRepair/repairUnionidSingle/
	 */
	public function repairUnionidSingle() {
		$db_perpage = 100; //每页条数
		$wid = 12;

		$weixinAccountDao = model('WeixinAccount');
		$wechatUserDao = model('WechatUser');

		$p = input('param.p', '1', 'intval');
		$suc = input('param.suc', '', 'intval');

		//读取用户数据
		$map = [
			'wid'		=> $wid,
			'unionid'	=> '',
		];

		$Page = new \org\util\PageBootstrap($count, config('app.per_page'), $parameter);
		list($list) = $weixinUserDao->search($map, $Page->getLimit());
		//print_r($list);exit;
		$count = count($list);
		//echo $count;exit;

		//获取公众号信息
		$wxdb = $weixinAccountDao->getInfo($wid);

		$options = array(
			'token'=>$wxdb['token'], //填写你设定的key
			'encodingaeskey'=>$wxdb['aeskey'], //填写加密用的EncodingAESKey
			'appid'=>$wxdb['appid'], //填写高级调用功能的app id, 请在微信开发模式后台查询
			'appsecret'=>$wxdb['appsecret'], //填写高级调用功能的密钥
			'partnerid'=>'', //财付通商户身份标识，支付权限专用，没有可不填
			'partnerkey'=>'', //财付通商户权限密钥Key，支付权限专用
			'paysignkey'=>'' //商户签名密钥Key，支付权限专用
		);
		$weObj = new \Common\Lib\Util\wechat($options);

		$user_list = [];
		foreach ($list as $key => $value) {
			if ($value['unionid']) { //过滤掉有信息的用户
				continue;
			}

			$result = $weObj->getUserInfo($value['openid']); //获取关注者详细信息
			if ($result == false) {
				echo '失败，错误代码：'.$weObj->errCode.'；错误提示：'.$weObj->errMsg;exit;
			}
			//print_r($result);exit;

			$wxUserInfo = $result;
			//print_r($wxUserInfo);exit;

			//$resultUserList = $result['user_info_list'];
			//foreach ($resultUserList as $key => $wxUserInfo) {
				$data = array(
					'nickname'			=> $wxUserInfo['nickname'],
					'sex' 				=> $wxUserInfo['sex'],
					'city' 				=> $wxUserInfo['city'],
					'province' 			=> $wxUserInfo['province'],
					'country' 			=> $wxUserInfo['country'],
					'headimgurl' 		=> $wxUserInfo['headimgurl'],
					'subscribe_time'	=> $wxUserInfo['subscribe_time'],
					'unionid'			=> $wxUserInfo['unionid'],
				);
				//更新每个用户(根据wid和openid更新)
				$wechatUserDao->where(array('wid'=>$wid, 'openid'=>$wxUserInfo['openid']))->data($data)->save();
			//}
		}

		if ($count > 0) {
			$p++;
			$this->wapSuccess('同步该页数据成功共同步'.$count.'个数据，成功'.$suc.'个数据，等待跳转', url('weixin_manage/wechatRepair/repairUnionidSingle', ['p'=>$p, 'suc'=>$suc]));
		} else {
			$this->wapSuccess('同步小区数据成功');
		}
	}

}